package com.hongzhimei.yukicomic.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.hongzhimei.yukicomic.R;
import com.hongzhimei.yukicomic.adapter.WelfareNewivalsAdapter;
import com.hongzhimei.yukicomic.adapter.WelfareTimeAdapter;
import com.hongzhimei.yukicomic.utils.GlideImageLoader;
import com.hongzhimei.yukicomic.utils.ToastUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Nick CY on 2016/12/20 15:14
 * 邮箱：fsi000044@163.com
 * 福利仓
 */

public class WelfareFrament extends BaseFragment {
    //购物车
    @Bind(R.id.fragment_cwelfare_addArticle)
    ImageView fragmentCwelfareAddArticle;
    //轮播图
    @Bind(R.id.fragment_welfare_banner)
    Banner fragmentWelfareBanner;
    //8 个分类
    @Bind(R.id.welfare_img_1)
    ImageView welfareImg1;
    @Bind(R.id.welfare_txt_1)
    TextView welfareTxt1;
    @Bind(R.id.welfare_img_2)
    ImageView welfareImg2;
    @Bind(R.id.welfare_txt_2)
    TextView welfareTxt2;
    @Bind(R.id.welfare_img_3)
    ImageView welfareImg3;
    @Bind(R.id.welfare_txt_3)
    TextView welfareTxt3;
    @Bind(R.id.welfare_img_4)
    ImageView welfareImg4;
    @Bind(R.id.welfare_txt_4)
    TextView welfareTxt4;
    @Bind(R.id.welfare_img_5)
    ImageView welfareImg5;
    @Bind(R.id.welfare_txt_5)
    TextView welfareTxt5;
    @Bind(R.id.welfare_img_6)
    ImageView welfareImg6;
    @Bind(R.id.welfare_txt_6)
    TextView welfareTxt6;
    @Bind(R.id.welfare_img_7)
    ImageView welfareImg7;
    @Bind(R.id.welfare_txt_7)
    TextView welfareTxt7;
    @Bind(R.id.welfare_img_8)
    ImageView welfareImg8;
    @Bind(R.id.welfare_txt_8)
    TextView welfareTxt8;
    //倒计时
    @Bind(R.id.welfare_time)
    TextView welfareTime;
    //广告图
    @Bind(R.id.fragment_welfare_Advertisement_img)
    ImageView fragmentWelfareAdvertisementImg;
    //打折列表
    @Bind(R.id.welfare_time_recyclerview)
    RecyclerView welfareTimeRecyclerview;
    //新品推荐
    @Bind(R.id.welfare_newarrivals_recyclerview)
    RecyclerView welfareNewarrivalsRecyclerview;
    //热门商品
    @Bind(R.id.welfare_selling_recyclerview)
    RecyclerView welfareSellingRecyclerview;
    @Bind(R.id.fragment_cwelfare_pulltoscrollview)
    PullToRefreshScrollView fragmentCwelfarePulltoscrollview;
    private WelfareTimeAdapter welfareTimeadapter;
    private WelfareNewivalsAdapter welfareNewivalsadapter;

    //当 fragment不可见时执行改方法
    @Override
    public void onInvisible() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_welfare;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        //pullTorefresh设置
        pullTorefreshSelling();
        //轮播图设置
        BannerTime();
        //打折recyclerView的配置
        TimeRecyclerViewLoading();
        //新品recyclerView的配置
        NewarrivalsRecyclerview();
        //热门recyclerview的配置
        SellingRecyclerview();
    }

    private void pullTorefreshSelling() {
        fragmentCwelfarePulltoscrollview.setMode(PullToRefreshBase.Mode.BOTH);
        fragmentCwelfarePulltoscrollview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                ToastUtils.show(getActivity(),"刷新完毕");
                fragmentCwelfarePulltoscrollview.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                ToastUtils.show(getActivity(),"加载完毕");
                fragmentCwelfarePulltoscrollview.onRefreshComplete();
            }
        });
    }

    private void BannerTime() {
        //初始化banner
        fragmentWelfareBanner.setImageLoader(new GlideImageLoader());
        fragmentWelfareBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //假数据
        List<String> images = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        images.add("http://ww1.sinaimg.cn/large/006eg253jw1fay8b6bbf4j30hs0hsabh.jpg");
        images.add("http://ww2.sinaimg.cn/large/0066cVpfjw1fay6kw3kbij30hs0hsn1r.jpg");
        images.add("http://ww3.sinaimg.cn/large/0066d6AWjw1faw40fd1wsj30hs0hsgn3.jpg");
        images.add("http://ww2.sinaimg.cn/large/0066cYcyjw1fay6a523a7j30hy0ciwja.jpg");
        titles.add("1月新番那么多，分分钟就告诉你有哪些好看！（上）");
        titles.add("天凉了，下雪了，送自己一辆兰博基尼吧");
        titles.add("上车吧！蛋黄带你萌穿越回去看上个世纪七十年代的妹子是啥样的");
        titles.add("被女票问到底有多长");
        fragmentWelfareBanner.setImages(images).setBannerTitles(titles).setBannerAnimation(Transformer.Tablet).setDelayTime(2000).start();
        //广告图
        Glide.with(getActivity()).load("http://imga1.pic21.com/bizhi/140114/06569/s03.jpg").into(fragmentWelfareAdvertisementImg);
    }

    private void SellingRecyclerview() {
        List<String> newlist = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            newlist.add("http://ww3.sinaimg.cn/large/0066bYqcjw1faxlreqkbhj306o06ojrr.jpg");
        }
        welfareSellingRecyclerview.setNestedScrollingEnabled(false);
        welfareSellingRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        welfareNewivalsadapter = new WelfareNewivalsAdapter(getActivity(), newlist);
        welfareSellingRecyclerview.setAdapter(welfareNewivalsadapter);
        //item点击事件
        welfareNewivalsadapter.setOnItemClickLitener(new WelfareTimeAdapter.onItemClickLitener() {
            @Override
            public void onItemClick(View view, int postion) {
                ToastUtils.show(getActivity(),"第"+postion+"item被点击");
            }
        });
    }

    private void NewarrivalsRecyclerview() {
        List<String> newlist = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            newlist.add("http://ww1.sinaimg.cn/large/005Pc3gmjw1f6hjr650kyj305k05kaa4.jpg");
        }
        welfareNewarrivalsRecyclerview.setNestedScrollingEnabled(false);
        welfareNewarrivalsRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(),2));
        welfareNewivalsadapter = new WelfareNewivalsAdapter(getActivity(), newlist);
        welfareNewarrivalsRecyclerview.setAdapter(welfareNewivalsadapter);
        //item点击事件
        welfareNewivalsadapter.setOnItemClickLitener(new WelfareTimeAdapter.onItemClickLitener() {
            @Override
            public void onItemClick(View view, int postion) {
                ToastUtils.show(getActivity(),"第"+postion+"item被点击");
            }
        });
    }

    private void TimeRecyclerViewLoading() {
        List<String> timelist = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            timelist.add("http://ww4.sinaimg.cn/large/006fsnxMjw1faybrp7ubdj306o06omxo.jpg");
        }
        //设置打折页面横向滑动
        LinearLayoutManager linearmanager = new LinearLayoutManager(getActivity());
        linearmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
        welfareTimeRecyclerview.setLayoutManager(linearmanager);
        welfareTimeadapter = new WelfareTimeAdapter(getActivity(), timelist);
        welfareTimeRecyclerview.setAdapter(welfareTimeadapter);
        //item点击事件
        welfareTimeadapter.setOnItemClickLitener(new WelfareTimeAdapter.onItemClickLitener() {
            @Override
            public void onItemClick(View view, int postion) {
                ToastUtils.show(getActivity(),"第"+postion+"item被点击");
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
